/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo.Controler;

import Model.Beans.Book;
import Model.Beans.Tag;
import Model.Servicios.BookSrv;
import Model.Utilities.Constantes.BookState;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author Manuel Lap
 */
public class MgmtBookAction extends ActionSupport{
    
    private Book book = new Book();
    private BookSrv srvBook = new BookSrv();
    private String tags ;
    
    
   
    public String registroLibro(){
        String tagsList[] = tags.split(" ");
        Set lista = (Set) new HashSet<Tag>(convertToArray(tagsList));
        book.setTags(lista);
        book.setStatus(BookState.APROBADO);
        srvBook.addBookTags(book);
        return "";
    }
    
    private ArrayList<Tag> convertToArray(String tags[]){
        ArrayList<Tag> lista = new ArrayList<Tag>();
        for(int i = 0; i < tags.length; i++){
            Tag tag = new Tag();
            tag.setTag(tags[i]);
            lista.add(tag);
        }
        return lista;
    }
    
    @Override
    public void validate(){
        if (getBook() != null) {
            if (!(getBook().getName().compareTo("") != 0)) {
                addFieldError("book.name", "Faltan nombre");
            }
            if (!(getBook().getAuthor().compareTo("") != 0)) {
                addFieldError("book.author", "Falta autor");
            }
            if (!(getBook().getEditorial().compareTo("") != 0)) {
                addFieldError("book.editorial", "Falta editorial");
            }
            if (!(getBook().getIsbn().compareTo("") != 0)) {
                addFieldError("studentBean.user.password", "Falta isbn");
            }
            if (!(getBook().getYear() != 0)) {
                addFieldError("book.year", "Falta año");
            }
        }
    }

    /**
     * @return the book
     */
    public Book getBook() {
        return book;
    }

    /**
     * @param book the book to set
     */
    public void setBook(Book book) {
        this.book = book;
    }

    /**
     * @return the tags
     */
    public String getTags() {
        return tags;
    }

    /**
     * @param tags the tags to set
     */
    public void setTags(String tags) {
        this.tags = tags;
    }
}
